﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace DCIMAP.GANS.GeneticAlgorithms.Legacy
{
    /// <summary>
    /// Panel for representation of SetPartition task
    /// </summary>
    public class IQOutputPanel : TextBox, IGeneticAlgorithmOutputControl
    {
        private readonly int[] weights;

        /// <summary>
        /// Creates an instance of SetPartitionOutputPanel with specified array of items' weights
        /// </summary>
        public IQOutputPanel(int[] weights)
        {
            this.weights = weights;
            ReadOnly = true;
            Multiline = true;
            BackColor = Color.White;
            ScrollBars = ScrollBars.Vertical;
            Font = new Font("Times New Roman", 13);
        }


        /// <inheritdoc/>
        public void Draw(Gene g)
        {
            string st1 = "";
            string st2 = "";
            int S1 = 0;
            int S2 = 0;
            StringArrayGene ar = (StringArrayGene)g;

            for (int i = 0; i < ar.Code.Length; i++)
                if (ar.StringCode[i]=="1")
                {
                    st1 += weights[i] + "+";
                    S1 += weights[i];
                }
                else
                {
                    st2 += weights[i] + "+";
                    S2 += weights[i];
                }

            if (st1 == "")
                st1 = "0+";
            if (st2 == "")
                st2 = "0+";
            st1 = st1.Substring(0, st1.Length - 1) + "=" + S1;
            st2 = st2.Substring(0, st2.Length - 1) + "=" + S2;
            Text = "Defect: " + (Math.Abs(S1 - S2)) + "\r\n\r\n" + st1 + "\r\n\r\n" + st2;
        }

    }
}